#include "head.h"
#include <sys/stat.h>                                                           
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include "protocol.h" 
static pthread_mutex_t mutex_link_zigbee;

#define DEVICE "/dev/ttyUSB"

int uart_init(int arg, int baud)
{
    int fd;
    char port[20];
    struct termios Opt;
    int uartbiit[50]= {B115200,B9600,B19200,B4800,B2400,B1200};

    memset(port , 0 , sizeof(port));
    memcpy(port , DEVICE , sizeof(DEVICE));
    port[sizeof(DEVICE)-1] = 0x30 + arg;
    fd = open(port , O_RDWR);     //打开串口
    if (fd<0)
    {
        printf("cant open %s\n" , port);
        return -1;                 //没有打开返回
    }
    else
    {
        printf("open %s\n" , port);
    }

    tcgetattr(fd,&Opt);      //初始化
    Opt.c_cflag |= (CLOCAL | CREAD);
    Opt.c_cflag &= ~CSIZE;
    Opt.c_cflag &= ~CRTSCTS;
    Opt.c_cflag |= CS8;
    Opt.c_cflag &= ~CSTOPB;
    Opt.c_iflag |= IGNPAR;
    Opt.c_oflag = 0;
    Opt.c_lflag = 0;
    cfsetispeed(&Opt,uartbiit[baud]);    //设置波特率
    cfsetospeed(&Opt,uartbiit[baud]);
    tcflush(fd,TCIFLUSH);

    if (tcsetattr(fd,TCSANOW,&Opt) != 0)       //装载初始化参数
    {
        perror("Setup Serial fail;\n");
        close(fd);
        return -1;
    }
    return(fd);
}

void *zigbee_recv_handler(void *arg)
{
    int fd, i=0;
    unsigned char buf[TEXT_MAX + 1];
    struct linkmsg egMSG;
    unsigned int input_env;
    ssize_t sret;

	printf("*************zigbee_recv_handler*************\n");

    while(1)
    {

        pthread_mutex_lock(&mutex_link_zigbee);

        if((fd = uart_init(0, 0)) <0)   //打开串口，波特率为115200；
        {
            printf("Open uart err \n");
            return NULL;
        }

        while((buf[0] != 'E') && (buf[0] != 'G'))
        {
            sret = read(fd , buf , 1);
            if(sret < 0)
            {
                printf("file : %s\n, func : %s\n, line : %d\n" , \
                      __FILE__ , __func__ , __LINE__);
                perror("read zigbee fail;");
                exit(-1);
            }
        }

        i = 1;
        if(buf[0] == 'E')
        {
            input_env = sizeof(struct env_msg);
        }
        else
        {
            input_env = sizeof(struct good_msg);
        }

        while(i <= input_env)
        {
            sret = read(fd , buf + i , input_env - i + 1);
            if(sret < 0)
            {
                printf("file : %s\n, func : %s\n, line : %d\n" , \
                      __FILE__ , __func__ , __LINE__);
                perror("read USRT fail;");
                exit(-1);
            }
            i += (int)sret;
        }
        
        close(fd);
        pthread_mutex_unlock(&mutex_link_zigbee);

        memcpy((char *)(&egMSG) , buf , input_env + 1);

        pthread_mutex_lock(&mutex.mutex_link_data);
        input_linkqueue(&egMSG , link_data);
        /***********************************
        while(input_linkqueue(&linkmsg , link_data))
        {
            pthread_cond_wait(&cond_analysis , &mutex_link_data);
        }
        ***********************************/
        pthread_mutex_unlock(&mutex.mutex_link_data);
        pthread_cond_broadcast(&cond.cond_analysis);
    }
}

void *zigbee_send_handler(void *arg)
{
    int fd, i=0;
    unsigned char buf[TEXT_MAX + 1];
    struct linkmsg cmdMSG;
    struct A_msg * pAMSG;
    unsigned int input_env;
    ssize_t sret;

	printf("*************zigbee_recv_handler*************\n");
    while(1)
    {
        pthread_mutex_lock(&mutex.mutex_zigbee_send);
        while(output_linkqueue(link_cmd , &cmdMSG))
        {
            pthread_cond_wait(&cond.cond_zigbee_send , &mutex.mutex_zigbee_send);
        }
        pthread_mutex_unlock(&mutex.mutex_zigbee_send);

        pAMSG =(struct A_msg *)(&(cmdMSG.text));
        input_env = sizeof(struct A_msg);
        memcpy(buf , (char *)(pAMSG), input_env);//pAMSG用来后期加入校验

        pthread_mutex_lock(&mutex_link_zigbee);
        if((fd = uart_init(0, 0)) <0)   //打开串口，波特率为115200；
        {
            printf("Open uart err \n");
            return NULL;
        }

        i = 0;
        while(i < input_env)
        {
            sret = write(fd , buf + i , input_env - i);
            if(sret == -1)
            {
                printf("file : %s\n, func : %s\n, line : %d\n" , \
                       __FILE__ , __func__ , __LINE__);
                perror("write USRT fail;");
                exit(-1);
            }
            i += (int)sret;
        }

        close(fd);
		printf("zigbee  send is success\n");
        pthread_mutex_unlock(&mutex_link_zigbee);
    }
}

